package com.zk.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zk.yygh.hosp.repository.DepartmentRepository;
import com.zk.yygh.hosp.service.DepartmentService;
import com.zk.yygh.model.hosp.Department;
import com.zk.yygh.vo.hosp.DepartmentQueryVo;
import com.zk.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: 张凯
 * @date: 2022/6/4 - 17:35
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室
    @Override
    public void save(Map<String, Object> parameterMap) {
        //将参数转为科室对象
        Department department = JSONObject.parseObject(JSONObject.toJSONString(parameterMap), Department.class);
        //查询mongodb中是否存在对象
        Department departmentFind = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentFind!=null){
            //科室不为空则修改
            department.setCreateTime(departmentFind.getCreateTime());
        }else {
            department.setCreateTime(new Date());
        }
        department.setUpdateTime(new Date());
        department.setIsDeleted(0);
        departmentRepository.save(department);
    }

    //查询科室
    @Override
    public Page<Department> selectPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //1.创建pageable对象: 起始页为0
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");//通过时间降序
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        //2.创建example实例
        Department department=new Department(); //2.1 创建查询的对象
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        ExampleMatcher matcher=ExampleMatcher.matching()    //2.2 创建匹配器
        .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
        .withIgnoreCase(true);//忽略大小写
        Example<Department> example=Example.of(department,matcher);
        //3.进行分页查询
        Page<Department> pages = departmentRepository.findAll(example, pageable);
        return pages;
    }

    //删除科室
    @Override
    public void remove(String hoscode, String depcode) {
        //通hoscode和depcode进行查询科室
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        //执行删除: 前提是能够查询出来
        if (department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> getDepartmentTree(String hoscode) {
        //先查询该医院的所有科室
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example=Example.of(department);
        List<Department> scheduleList = departmentRepository.findAll(example);
        //将查询出的所有科室根据大科室进行分组
        Map<String, List<Department>> departmentCollect = scheduleList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //将分组的科室进行封装成树形数据
        List<DepartmentVo> departmentTree = new ArrayList<>();
        for(Map.Entry<String,List<Department>> entry:departmentCollect.entrySet()){
            //一个个的大科室封装
            DepartmentVo departmentNode = new DepartmentVo();
            List<Department> departmentsValue = entry.getValue();
            //设置大科室信息
            departmentNode.setDepcode(departmentsValue.get(0).getDepcode());
            departmentNode.setDepname(departmentsValue.get(0).getDepname());
            //设置其里面小科室信息
            List<DepartmentVo> children=new ArrayList<>();
            for(Department department1:departmentsValue){
                DepartmentVo departmentChild = new DepartmentVo();
                departmentChild.setDepcode(department1.getDepcode());
                departmentChild.setDepname(department1.getDepname());
                children.add(departmentChild);
            }
            departmentNode.setChildren(children);
            //给树形科室中添加节点
            departmentTree.add(departmentNode);
        }
        return departmentTree;
    }
}
